package bitmap
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Linear;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	[SWF(width="600",height="600",backgroundColor="#bdb8b8")]
	public class BitmapCut extends Sprite
	{
		[Embed(source="assets/img.png")]
		private var pic:Class;
		
		private var bmp:Bitmap;
		private var bmpd:BitmapData;
		
		private var sbmpd:BitmapData;
		
		private var timer:Timer;
		
		private var isJump:Boolean = false;
		private var isSuspension:Boolean = false;
		private var isSecJump:Boolean = false;
		
		private var horizon:Number;
		
		private var lastTime:int;
		
		private var bg:Sprite;
		
		public function BitmapCut()
		{
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.align = StageAlign.TOP_LEFT;
			init();
		}
		
		public function init():void {
			bg = new Sprite();
			this.addChild(bg);
			sbmpd = (new pic() as Bitmap).bitmapData;
			bmpd = new BitmapData(sbmpd.width/8,sbmpd.height/8);
			bmpd.copyPixels(sbmpd,new Rectangle(0,0,sbmpd.width/8,sbmpd.height/8),new Point(0,0));
			bmp = new Bitmap();
			bmp.bitmapData = bmpd;
			this.addChild(bmp);
			bmp.x = 200;
			bmp.y = 200;
			horizon = 200;
			
			this.stage.addEventListener(MouseEvent.CLICK,jump);
			run();
			
			
			var bgTimer:Timer = new Timer(3000);
			bgTimer.addEventListener(TimerEvent.TIMER,drawBg);
			bgTimer.start();
		}
		
		public function drawBg(event:TimerEvent):void {
			trace(this.stage.stageWidth+"...................."+stage.stageHeight);
			var ran:Number = Math.random()*10;
			var circle:Sprite = new Sprite();
			
			if(ran>5) {
				circle.graphics.beginFill(0xff0000);
				circle.graphics.drawCircle(0,0,30);
				circle.graphics.endFill();
			}else {
				circle.graphics.beginFill(0xff0000);
				circle.graphics.drawRect(0,0,30,30);
				circle.graphics.endFill();
			}
			this.bg.addChild(circle);
			circle.x = this.stage.width;
			circle.y = bmp.y;
			TweenLite.to(circle,10,{x:-100,y:bmp.y,ease:Linear.easeNone});
		}
		
		public function jump(event:MouseEvent):void {
			j = 0;
			if(isJump) {
				isSuspension = true;
				lastTime = getTimer();
				draw(3,1);
			}else {
				angle = 0;
				isJump = true;
				draw(1,120);
			}
		}
		
		public function run():void {
			j = 0;
			draw(0,42);
		}
		
		public function trundle():void {
			j = 0;
			i = 5
			if(timer)
				timer.removeEventListener(TimerEvent.TIMER,onTimer);
			timer = new Timer(100,8);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTrundleComplete);
			timer.start();
		}
		
		public function onTrundleComplete(event:TimerEvent):void {
			run();
		}
		
		public function draw(frame:int,time:int):void {
			i = frame;
			if(timer)
			timer.removeEventListener(TimerEvent.TIMER,onTimer);
			timer = new Timer(time);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
			timer.start();
		}
		
		private var i:int = 3;//行
		private var j:int = 0;//列
		private var angle:Number = 0;
		public function onTimer(event:TimerEvent=null):void {
				bmpd.copyPixels(sbmpd,new Rectangle(j*sbmpd.width/8,i*sbmpd.height/8,sbmpd.width/8,sbmpd.height/8),new Point(0,0));
				this.bmp.bitmapData = bmpd;
//				if(i==8) i=0;
				
				if(j<7) {
					j++;
				}else {
					j=0;
//					i++;
				}
				if(isJump) {
					bmp.y = horizon-Math.sin(angle)*80;
					if(angle == Math.PI) {
						timer.stop();
						isJump = false;
						trundle();
					}else {
						if(!isSuspension) {
							angle += Math.PI/8;
						}else {
							if(getTimer()-lastTime>500) {
								isSuspension = false;
							}
						}
					}
				}
		}
	}
}